<template>
  <!-- 角色表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="700px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="检测项目编号" prop="checkItemCode">
        <el-input
          v-model="formData.checkItemCode"
          placeholder="请输入检测项目编号"
          clearable
        />
      </el-form-item>
      <el-form-item label="检测项目名称" prop="checkItemName">
        <el-input
          v-model="formData.checkItemName"
          placeholder="请输入检测项目名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="检测类目">
        <el-tree-select
          v-model="formData.checkCategoryId"
          :multiple="false"
          check-strictly
          :props="{
            value: 'id',
            label: 'checkCategoryName',
            children: 'nodes',
          }"
          @change="treeChange"
          :data="categoryData"
          clearable
        />
      </el-form-item>
      <el-form-item label="检测方法" prop="checkItemMethod">
        <el-input
          v-model="formData.checkItemMethod"
          placeholder="请输入检测方法"
          clearable
        />
      </el-form-item>
      <el-form-item label="使用工具" prop="checkItemTool">
        <el-input
          v-model="formData.checkItemTool"
          placeholder="请输入使用工具"
          clearable
        />
      </el-form-item>
      <el-form-item label="是否必输项" prop="isRequired" style="width: 330px">
        <el-radio-group v-model="formData.isRequired">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="2">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="检查方式" prop="checkType">
        <el-select
          v-model="formData.checkType"
          placeholder="请选择检查方式"
          clearable
        >
          <el-option
            v-for="item in useDictionary('CheckType')"
            :key="item.value"
            :label="item.label"
            :value="Number(item.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标准值范围上限" prop="standardMax">
        <el-input-number
          v-model="formData.standardMax"
          placeholder="请输入标准值范围上限"
          :step="0.01"
        />
      </el-form-item>
      <el-form-item label="标准值范围下限" prop="standardMin">
        <el-input-number
          v-model="formData.standardMin"
          placeholder="请输入标准值范围下限"
          :step="0.01"
        />
      </el-form-item>
      <el-form-item label="是否录入数值" prop="inputValue" style="width: 330px">
        <el-radio-group v-model="formData.inputValue">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="2">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否检查数值" prop="checkValue" style="width: 330px">
        <el-radio-group v-model="formData.checkValue">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="2">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" style="width: 650px">
        <el-input
          type="textarea"
          v-model="formData.remarks"
          placeholder="请输入备注"
          clearable
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictionary } from "@/utils/useTable";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MDMCHECKSCHEMEITEMADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  schemeId: [{ required: true, message: "请输入方案id", trigger: "blur" }],
  schemeCode: [{ required: true, message: "请输入方案代码", trigger: "blur" }],
  checkItemCode: [
    { required: true, message: "请输入检测项目编号", trigger: "blur" },
  ],
  checkItemName: [
    { required: true, message: "请输入检测项目名称", trigger: "blur" },
  ],
  checkCategoryId: [
    { required: true, message: "请输入检测类目id", trigger: "blur" },
  ],
  checkItemMethod: [
    { required: true, message: "请输入检测方法", trigger: "blur" },
  ],
  checkItemTool: [
    { required: true, message: "请输入使用工具", trigger: "blur" },
  ],
  isRequired: [
    { required: true, message: "请输入是否必输项", trigger: "blur" },
  ],
  checkType: [{ required: true, message: "请输入检查方式", trigger: "blur" }],
  standardMax: [
    { required: true, message: "请输入标准值范围上限", trigger: "blur" },
  ],
  standardMin: [
    { required: true, message: "请输入标准值范围下限", trigger: "blur" },
  ],
  inputValue: [
    { required: true, message: "请输入是否录入数值", trigger: "blur" },
  ],
  checkValue: [
    { required: true, message: "请输入是否检查数值", trigger: "blur" },
  ],
  version: [{ required: true, message: "请输入数据版本号", trigger: "blur" }],
});

const formRef = ref(ElForm);
let categoryData = ref([]);
let formData = reactive({
  id: "",
  schemeId: "",
  schemeCode: "",
  schemeName: "",
  checkItemCode: "",
  checkItemName: "",
  checkCategoryId: "",
  checkItemMethod: "",
  checkItemTool: "",
  isRequired: "",
  checkType: "",
  standardMax: "",
  standardMin: "",
  inputValue: "",
  checkValue: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});
watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        schemeId,
        schemeCode,
        schemeName,
        checkItemCode,
        checkItemName,
        checkCategoryId,
        checkCategoryName,
        checkItemMethod,
        checkItemTool,
        isRequired,
        checkType,
        standardMax,
        standardMin,
        inputValue,
        checkValue,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        schemeId,
        schemeCode,
        schemeName,
        checkItemCode,
        checkItemName,
        checkCategoryId,
        checkCategoryName,
        checkItemMethod,
        checkItemTool,
        isRequired,
        checkType,
        standardMax,
        standardMin,
        inputValue,
        checkValue,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      });
    } else {
      const { schemeId, schemeCode, schemeName } = newVal;
      Object.assign(formData, {
        schemeId,
        schemeCode,
        schemeName,
      });
    }
    getCategoryData(formData.schemeId);
  }
);

function treeChange() {
  console.log("formData.checkCategoryId", formData.checkCategoryId);
  formData.checkCategoryName = categoryData.value.find((item) => {
    return item.id === formData.checkCategoryId;
  }).checkCategoryName;
  console.log("formData.checkCategoryName", formData.checkCategoryName);
}
/** 检测方案查询详细表 */
async function getCategoryData(id: string) {
  const data = await request({
    url: "/mdm/checkcategory/getList/" + id,
    method: "get",
  });
  categoryData.value = data.data;
}

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.schemeId = "";
  formData.schemeCode = "";
  formData.schemeName = "";
  formData.checkItemCode = "";
  formData.checkItemName = "";
  formData.checkCategoryId = "";
  formData.checkItemMethod = "";
  formData.checkItemTool = "";
  formData.isRequired = "";
  formData.checkType = "";
  formData.standardMax = "";
  formData.standardMin = "";
  formData.inputValue = "";
  formData.checkValue = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 检测方案项目保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mdm/checkschemeitem/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

/** 检测方案项目查询详细表 */
async function getDatails() {
  const data = await request({
    url: "/mdm/checkschemeitem/getDetails",
    method: "post",
    data: formData,
  });
  console.log(data);
}

onMounted(() => {
  if (formData.id) {
    getDatails();
  }
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
